define(['jquery'], function ($) {
  'use strict';
  function successCallback(result, options) {
    options.success && options.success(result);
  }
  function errorCallback(result, options) {
    options.error && options.error(result);
  }

  return function (options, file) {
    var env = '';
    var baseHeader = {};

    // 处理get请求
    var params = options.params || '';
    var addParams = '';
    if (params) {
      addParams = options.url.indexOf('?') !== -1 ? '&' : '?';
      for (var key in params) {
        if (Object.hasOwnProperty.call(params, key)) {
          var element = params[key];
          addParams = addParams + key + '=' + element + '&';
        }
      }
      addParams = addParams.substr(0, addParams.length - 1);
    }

    // 合并header
    if (options.headers) {
      for (var key in options.headers) {
        baseHeader[key] = options.headers[key];
      }
    }
    $.ajax({
      url: options.url.indexOf('https:') > -1 ? options.url + addParams : env + '/api' + options.url + addParams,
      type: options.method,
      headers: baseHeader,
      timeout: 20000,
      data: options.data ? JSON.stringify(options.data || '{}') : '',
      contentType: 'application/json;charset=utf-8',
      processData: true,
      success: function (result) {
        successCallback(result, options);
      },
      error: function (result) {
        errorCallback(result, options);
      },
    });
  };
});
